//
//  NoteReview.swift
//  hello
//
//  Created by 赵康 on 2025/3/30.
//

  // NoteReview.swift

import Vapor
import Fluent

final class NoteReview: Model, Content, @unchecked Sendable {
  static var schema: String { SchemaEnum.noteReviews.rawValue }
  
  @ID(key: .id)
  var id: UUID?
  
  @Parent(key: FieldKeys.note)
  var note: Note
  
  @Parent(key: FieldKeys.user)
  var user: User
  
    // 复习难度级别
  enum Difficulty: String, Codable {
    case again      // 重来
    case hard       // 困难
    case good       // 良好
    case easy       // 简单
    case ignored    // 不再复习
  }
  
  @Field(key: FieldKeys.difficulty)
  var difficulty: Difficulty
  
  @Field(key: FieldKeys.nextReviewDate)
  var nextReviewDate: Date?
  
  @Field(key: FieldKeys.reviewCount)
  var reviewCount: Int
  
  @Field(key: FieldKeys.lastReviewDate)
  var lastReviewDate: Date?
  
  // 新增: Anki算法相关字段
  @Field(key: FieldKeys.easeFactor)
  var easeFactor: Double
  
  @Field(key: FieldKeys.interval)
  var interval: Double
  
  // 学习阶段状态
  enum LearningStage: String, Codable {
    case new        // 新卡片
    case learning   // 学习中
    case reviewing  // 复习中
    case relearning // 重新学习中
    /**卡片处于复习阶段的前提下，用户选择了重来按钮，此时lapseCount加1，进入重学阶段
     重学阶段使用lapseSteps来设置时间间隔，而不是初始的learningSteps
     用户重学阶段选择Good后重新卡片进入复习阶段，但是遗忘次数lapseCount不会重置**/
    case suspended  // 暂停复习
  }
  
  @Field(key: FieldKeys.learningStage)
  var learningStage: LearningStage
  
  // 连续失败次数
  @Field(key: FieldKeys.lapseCount)
  var lapseCount: Int
  
  @Timestamp(key: FieldKeys.createdAt, on: .create)
  var createdAt: Date?
  
  @Timestamp(key: FieldKeys.updatedAt, on: .update)
  var updatedAt: Date?
  
  init() { }
  
  init(
    id: UUID? = nil, 
    noteID: Note.IDValue, 
    userID: User.IDValue,
    difficulty: Difficulty = .good, 
    nextReviewDate: Date? = nil,
    reviewCount: Int = 0, 
    lastReviewDate: Date? = nil,
    easeFactor: Double = 2.5,  // 初始默认值
    interval: Double = 0,      // 初始间隔(天)
    learningStage: LearningStage = .new,
    lapseCount: Int = 0
  ) {
    self.id = id
    self.$note.id = noteID
    self.$user.id = userID
    self.difficulty = difficulty
    self.nextReviewDate = nextReviewDate
    self.reviewCount = reviewCount
    self.lastReviewDate = lastReviewDate
    self.easeFactor = easeFactor
    self.interval = interval
    self.learningStage = learningStage
    self.lapseCount = lapseCount
  }
  
    // 更新复习状态请求
  struct Update: Content {
    let difficulty: Difficulty
  }
  
    // 复习响应
  struct Response: Content {
    let id: UUID?
    let noteID: UUID
    let difficulty: Difficulty
    let nextReviewDate: Date?
    let reviewCount: Int
    let lastReviewDate: Date?
    let easeFactor: Double
    let interval: Double
    let learningStage: LearningStage
    let lapseCount: Int
    let createdAt: Date?
    let updatedAt: Date?
  }
  
    // 用户复习设置
  struct Settings: Content {
    let dailyReviewCount: Int
    let prioritizeRecent: Bool
  }
  
    // 增强的复习笔记响应，包含不同难度的下次复习时间预测
  struct ReviewNoteResponse: Content {
    let id: UUID?
    let content: String
    let timestamp: Double?
    let tags: [String]?
    let userID: UUID
    let audioID: UUID
    let answer: String?
    let isArabic: Bool
    let isAnswerArabic: Bool
    let createdAt: Date?
    let updatedAt: Date?
    
      // 不同难度的下次复习时间预测
    let nextReviewDates: NextReviewDates
    
      // 当前复习状态信息
    let currentReview: ReviewInfo?
    
    struct NextReviewDates: Content {
      let again: Date?
      let hard: Date?
      let good: Date?
      let easy: Date?
    }
    
    struct ReviewInfo: Content {
      let difficulty: NoteReview.Difficulty
      let learningStage: NoteReview.LearningStage
      let interval: Double
      let easeFactor: Double
      let reviewCount: Int
      let lapseCount: Int
    }
  }
  
  struct ToggleSuspendResponse: Content {
    let success: Bool
    let message: String
    let suspended: Bool
  }
  
  struct ReviewButtonStats: Content {
    let learning: [String: Int]
    let needsWork: [String: Int]
    let mastered: [String: Int]
  }
  
  
  struct ReviewStats: Content {
    let todayReviewCount: Int
    let tomorrowReviewCount: Int
    let weekReviewCount: Int
    let totalNoteCount: Int
    let completedNoteCount: Int
    let learningCount: Int
    let relearningCount: Int
  }
  
  // 添加预测复习量响应模型
  struct ReviewPrediction: Content {
    // 日期预测，以ISO8601日期字符串为键
    let predictions: [String: Int]
    // 汇总信息
    let weeklyTotal: Int
    let monthlyTotal: Int
    let totalCards: Int  // 添加总卡片数字段
  }
}

